<?php
namespace app\common\helper\printer\Yly;
use App\Api\PrinterService;
use App\Api\PrintService;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;

class YlyPrinter{
    protected  $config;

    public function __construct($config=[])
    {
        $this->config=$config;
    }


    public function OauthPrinter($client_id,$client_secret)
    {
        $config=new YlyConfig($client_id,$client_secret);
        $client = new YlyOauthClient($config);
        $token = $client->getToken();
        return $token;
    }

    public function addPrinter($machine_code,$msign,$printer_name)
    {
        $config=new YlyConfig($this->config['client_id'],$this->config['client_secret']);
        $printer = new PrinterService($this->config['access_token'], $config);
        $data = $printer->addPrinter($machine_code, $msign, $printer_name);
        return $data;
    }

    public function printMsg($machine_code,$content)
    {
        $origin_id=time().rand(10000,99999);
        $config=new YlyConfig($this->config['client_id'],$this->config['client_secret']);
        $print=new PrintService($this->config['access_token'],$config);
        $print->index($machine_code,$content,$origin_id);
        return true;
    }

    public function error($message){
        throw new \Exception($message);
    }

}